﻿using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Web;
using EPiServer.Common.Cache;

namespace JoelAbrahamsson.TestUtilities
{
    public class EPiServerCommonSetup
    {
        public static void InitCache(HttpContext context)
        {

            Type type = typeof(CacheHandler);

            FieldInfo infoCache = type.GetField("m_cache", BindingFlags.NonPublic | BindingFlags.Static);

            infoCache.SetValue(null, HttpContext.Current.Cache);

            FieldInfo info = type.GetField("m_cacheExpirationProvider", BindingFlags.NonPublic | BindingFlags.Static);

            CacheExpirationProvider provider = new CacheExpirationProvider();

            provider.Initialize(new NameValueCollection());

            info.SetValue(null, provider);

        }



        public static void InitCacheAndHttpContextWithAClearCache()
        {
            if (HttpContext.Current != null)
                HttpContextSetup.ClearCurrentContextCache();

            else
                HttpContextSetup.InitCurrentHttpContext();

            InitCache(HttpContext.Current);
        }
    }
}
